﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Iori.FileManageLib;
using Iori.DictCN;
using System.IO;


namespace WuLongAssistance {
    delegate WuLongFile GetWuLongFile();

    public partial class frmMain : Form {

        private readonly string fileName = "Private\\20003b40\\NewWord.txt";

        private WuLongFilesColletion wlFiles = new WuLongFilesColletion();

        private WordEC wordEC;

        public frmMain() {
            InitializeComponent();

        }

        private void frmMain_Load(object sender, EventArgs e) {

            lbMain.Items.AddRange(wlFiles.Files.ToArray());
            if (lbMain.Items.Count > 0) {
                lbMain.SelectedIndex = 0;
            }

            cbbDisk.Items.AddRange(GetDisks().ToArray());
            if (cbbDisk.Items.Count > 0) {
                cbbDisk.SelectedIndex = 0;
            }

            WuLongFile wlfile = ((WulongTxtInfor)lbMain.SelectedItem).OpenFile();
        }

        private List<string> GetDisks() {
            List<string> ret = new List<string>();
            string[] DiskList = Environment.GetLogicalDrives();
            foreach (string curDisk in DiskList) {
                if (System.IO.File.Exists(curDisk + fileName)) {
                    ret.Add(curDisk);
                }
            }
            return ret;
        }

        private void btnRefresh_Click(object sender, EventArgs e) {
            cbbDisk.Items.Clear();
            cbbDisk.Items.AddRange(GetDisks().ToArray());
            if (cbbDisk.Items.Count > 0) {
                cbbDisk.SelectedIndex = 0;
            }
        }

        private void btnCopy_Click(object sender, EventArgs e) {
            if (cbbDisk.Items.Count > -1) {
                WuLongFile wlfile = ((WulongTxtInfor)lbMain.SelectedItem).OpenFile();
                try {
                    wlfile.SaveAsELearningFile(cbbDisk.SelectedItem.ToString() + fileName, chbAppend.Checked, chbDistanctWords.Checked);
                    //wlfile.SaveAsELearningFile("C:\\fff.txt", chbAppend.Checked, chbDistanctWords.Checked);
                    MessageBox.Show("Done!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                } catch (Exception exc) {
                    MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            } else {
                MessageBox.Show("Disk Not Found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region Asynch AddToDB
        //TO-DO: delegate WuLongFile GetWuLongFile();

        private void btnAddToDB_Click(object sender, EventArgs e) {
            this.Cursor = Cursors.WaitCursor;
            btnAddToDB.Enabled = false;

            pbMain.Maximum = 1000;
            pbMain.Value = 0;
            ssMain.Visible = true;

            bwMain.RunWorkerAsync();

        }

        private void bwMain_DoWork(object sender, DoWorkEventArgs e) {
            BackgroundWorker work = sender as BackgroundWorker;
            AddToDB(work, e);
        }

        private void AddToDB(BackgroundWorker worker, DoWorkEventArgs e) {
            //代理从主线程获得WuLongFile;
            WuLongFile wlfile;
            if (!lbMain.InvokeRequired) {
                wlfile = ((WulongTxtInfor)lbMain.SelectedItem).OpenFile();
            } else {
                GetWuLongFile getNow = new GetWuLongFile(delegate {
                    return ((WulongTxtInfor)lbMain.SelectedItem).OpenFile();
                });
                wlfile = (WuLongFile)lbMain.Invoke(getNow);
            }

            Iori.BLL.TWORDS wordOper = new Iori.BLL.TWORDS();
            //向库添加并报告进度
            for (int i = 0; i < wlfile.Items.Count; i++) {
                wordOper.Add(wlfile.Items[i].Word, wlfile.Items[i].Type, wlfile.Items[i].Mean);
                worker.ReportProgress((int)(i * 1000 / wlfile.Items.Count));
            }

        }

        private void bwMain_ProgressChanged(object sender, ProgressChangedEventArgs e) {
            pbMain.Value = e.ProgressPercentage;
        }

        private void bwMain_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
            pbMain.Value = pbMain.Maximum;
            this.Cursor = Cursors.Default;
            btnAddToDB.Enabled = true;
            ssMain.Visible = false;

            MessageBox.Show("Done!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        #endregion

        private void btnOpenWordList_Click(object sender, EventArgs e) {
            WuLongFile wlfile = ((WulongTxtInfor)lbMain.SelectedItem).OpenFile();

            WordListView wlv = new WordListView(wlfile.Items);
            wlv.ShowDialog();

        }
    }
}